package com.chinatelecom.ifree.business.configcenter.helper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.chinatelecom.ifree.business.configcenter.ZkHelp;
import com.github.zkclient.IZkDataListener;

public class MysqlJdbcHelper {
	private final static Logger logger = LoggerFactory.getLogger(MysqlJdbcHelper.class);
	private final static String jdbcURLZkPath = "/ifree/mysql/jdbc";
	private final static String userNameZkPath = "/ifree/mysql/username";
	private final static String passwdZkPath = "/ifree/mysql/passwd";
	private String jdbcUrl = null;
	private String username = null;
	private String passwd = null;
	private ZkHelp zkHelp = ZkHelp.getInstance();
	private static MysqlJdbcHelper instance = null;
	private static IZkDataListener listenerURL = null;
	private static IZkDataListener listenerUser = null;
	private static IZkDataListener listenerPWD = null;

	private MysqlJdbcHelper() {
		// mysql URL
		listenerURL = new IZkDataListener() {
			@Override
			public void handleDataDeleted(String dataPath) throws Exception {
				logger.info("!!! rsVersion node data has been deleted !!!" + dataPath);
			}

			@Override
			public void handleDataChange(String dataPath, byte[] data) throws Exception {
				logger.info("!!! rsVersion node data has been changed !!!" + dataPath);
				String rtdata = null;
				if (data != null && data.length > 0) {
					rtdata = new String(data, "UTF-8");
					jdbcUrl = rtdata;
				}
				logger.info("!!! rsVersion node data has been changed ok !!!" + rtdata);
			}
		};
		// 添加节点监控
		zkHelp.subscribeDataChanges(jdbcURLZkPath, listenerURL);
		jdbcUrl = new String(zkHelp.getValue(jdbcURLZkPath));

		// mysql username
		listenerUser = new IZkDataListener() {
			@Override
			public void handleDataDeleted(String dataPath) throws Exception {
				logger.info("!!! rsVersion node data has been deleted !!!" + dataPath);
			}

			@Override
			public void handleDataChange(String dataPath, byte[] data) throws Exception {
				logger.info("!!! rsVersion node data has been changed !!!" + dataPath);
				String rtdata = null;
				if (data != null && data.length > 0) {
					rtdata = new String(data, "UTF-8");
					username = rtdata;
				}
				logger.info("!!! rsVersion node data has been changed ok !!!" + rtdata);
			}
		};
		// 添加节点监控
		zkHelp.subscribeDataChanges(userNameZkPath, listenerUser);
		username = new String(zkHelp.getValue(userNameZkPath));

		// mysql passwd
		listenerPWD = new IZkDataListener() {
			@Override
			public void handleDataDeleted(String dataPath) throws Exception {
				logger.info("!!! rsVersion node data has been deleted !!!" + dataPath);
			}

			@Override
			public void handleDataChange(String dataPath, byte[] data) throws Exception {
				logger.info("!!! rsVersion node data has been changed !!!" + dataPath);
				String rtdata = null;
				if (data != null && data.length > 0) {
					rtdata = new String(data, "UTF-8");
					passwd = rtdata;
				}
				logger.info("!!! rsVersion node data has been changed ok !!!" + rtdata);
			}
		};
		// 添加节点监控
		zkHelp.subscribeDataChanges(passwdZkPath, listenerPWD);
		passwd = new String(zkHelp.getValue(passwdZkPath));
		if (logger.isInfoEnabled())
			logger.info("===================init MysqlJdbcHelper ok================");
	}

	public static MysqlJdbcHelper getInstance() {
		if (instance == null) {
			synchronized (MysqlJdbcHelper.class) {
				instance = new MysqlJdbcHelper();
			}
		}
		return instance;
	}

	public String getJdbcUrl() {
		return jdbcUrl;
	}

	public void setJdbcUrl(String jdbcUrl) {
		this.jdbcUrl = jdbcUrl;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPasswd() {
		return passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}
}
